<template>
	<view  class="padding-top">
		<cu-custom bgColor="bg-top" :isBack="true">
			<block slot="content">{{name}}</block>
		</cu-custom>
		<form class="" @submit="formSubmit">
			<view class="card-menu cu-list menu">
				<view class="cu-item ">
					<view class="content">
						<text class="text-orange cuIcon-roundrightfill"></text>中转站设备状态
					</view>
				</view>
			</view>
			<view class="card-menu cu-list menu" >
				<view class="cu-form-group margin-top">
					<view class="title">风机状态</view>
					<switch @change="Switch1" :class="gongkuangFormData.switchy01001?'checked':''" :checked="gongkuangFormData.switchy01001?true:false"></switch>
				</view>
				<view class="padding-lr padding-tb-xs">
					<view class="uni-flex uni-row">
						<view class="flex-item-30">风机电流</view>
						<view class="flex-item-70">{{gongkuangFormData.y01001Rtd}}</view>
					</view>
				</view>
				<view class="cu-form-group margin-top">
					<view class="title">水泵状态</view>
					<switch @change="Switch2" :class="gongkuangFormData.switchy01002?'checked':''" :checked="gongkuangFormData.switchy01002?true:false"></switch>
				</view>
				<view class="padding-lr padding-tb-xs">
					<view class="uni-flex uni-row">
						<view class="flex-item-30">水泵电流</view>
						<view class="flex-item-70">{{gongkuangFormData.y01002Rtd}}</view>
					</view>
				</view>
				<view class="cu-form-group margin-top">
					<view class="title">UV状态</view>
					<switch @change="Switch3" :class="gongkuangFormData.switchy01003?'checked':''" :checked="gongkuangFormData.switchy01003?true:false"></switch>
				</view>
				<view class="padding-lr padding-tb-xs">
					<view class="uni-flex uni-row">
						<view class="flex-item-30">UV电流</view>
						<view class="flex-item-70">{{gongkuangFormData.y01003Rtd}}</view>
					</view>
				</view>
			</view>
			
		</form>
	</view>
</template>

<script>
	import Util from '@/utils/util.js'
	export default {
		components: {
		},
		data() {
			return {
				TabCur: 0,
				name:'',
				mn:'',
				gongkuangTimer:null,
				gongkuangSite: {},
				gongkuangData: [],
				gongkuangFormData: {},
				fankongSite: {},
			};
		},
		onLoad(e) {
			//加载垃圾中转站
			if(e.name){
				this.name = e.name
			}
			if(e.companyId){
				Util.request('/monitor/companySite/list', {companyID:e.companyId}, 'GET').then(res=>{
					res.data.records.map(item => {
						if(item.rowType === 'gongkuang'){
						    this.gongkuangSite = item
							Util.request('/monitor/companySite/get', {id:this.gongkuangSite.guid}, 'GET').then(res=>{
								this.gongkuangData = JSON.parse(res.data.siteLastData)
								this.initParams()
							})
						    //查询工况实时数据
						    //轮询获取工况实时数据
						    this.gongkuangTimer = setInterval( () => {
						    setTimeout(()=>{
								Util.request('/monitor/companySite/get', {id:this.gongkuangSite.guid}, 'GET').then(res=>{
									this.gongkuangData = JSON.parse(res.data.siteLastData)
									this.initParams()
								})
						    }, 0)
						    }, 30000)
						    }else if(item.rowType === 'fankong'){
						        this.fankongSite = item
						    }
					})
				}).catch(()=>{
					//联网失败, 结束加载
				})
			}
		},
		onHide() {
			clearInterval(this.gongkuangTimer)
		},
		methods:{
			Switch1(e) {
				this.gongkuangFormData.switchy01001 = e.detail.value
				if(e.detail.value){
					this.gongkuangFormData.y01001 = '1'
				}else{
					this.gongkuangFormData.y01001 = '0'
				}
				this.sendFankong()
			},
			Switch2(e) {
				this.gongkuangFormData.switchy01002 = e.detail.value
				if(e.detail.value){
					this.gongkuangFormData.y01002 = '1'
				}else{
					this.gongkuangFormData.y01002 = '0'
				}
				this.sendFankong()
			},
			Switch3(e) {
				this.gongkuangFormData.switchy01003 = e.detail.value
				if(e.detail.value){
					this.gongkuangFormData.y01003 = '1'
				}else{
					this.gongkuangFormData.y01003 = '0'
				}
				this.sendFankong()
			},
			sendFankong () {
			    //组织cp
			    this.gongkuangFormData.deviceCode = 'y01001='+this.gongkuangFormData.y01001+';y01002='+this.gongkuangFormData.y01002+';y01003='+this.gongkuangFormData.y01003
			
				let data = {
					mn: this.gongkuangFormData.mn,
					deviceCode: this.gongkuangFormData.deviceCode,
					deviceModel: 'ZhongZhiLJ',
					cmd: 'sendCmd',
				  }
			  
				Util.requestFankong('/fankong/device/exec', data, 'POST').then(res=>{
					uni.showToast({
						title: '命令发送成功',
						icon: 'success'
					})
				})
			},
			initParams(){
				this.gongkuangFormData = {
				    mn:this.fankongSite.siteMn,
				    deviceCode:'',
				    deviceModel:'ZhongZhiLJ',
				    cmd:'sendCmd',
					switchy01001: false,
					switchy01002: false,
					switchy01003: false,
				    y01001:'0',
				    y01002:'0',
				    y01003:'0',
				    y01001Rtd:'',
				    y01002Rtd:'',
				    y01003Rtd:'',
				}
				this.gongkuangData.map(item => {
					if(item.pollName === '风机状态'&&item.Rtd === 1){
						this.gongkuangFormData.y01001 = '1'
						this.gongkuangFormData.switchy01001 = true
					}else if(item.pollName === '水泵状态'&&item.Rtd === 1){
						this.gongkuangFormData.y01002 = '1'
						this.gongkuangFormData.switchy01002 = true
					}else if(item.pollName === 'UV状态'&&item.Rtd === 1){
						this.gongkuangFormData.y01003 = '1'
						this.gongkuangFormData.switchy01003 = true
					}else if(item.pollName === 'UV电流'){
						this.gongkuangFormData.y01003Rtd = item.Rtd
					}else if(item.pollName === '水泵电流'){
						this.gongkuangFormData.y01002Rtd = item.Rtd
					}else if(item.pollName === '风机电流'){
						this.gongkuangFormData.y01001Rtd = item.Rtd
					}
				})
			},
			tabSelect(e) {
				this.TabCur = e.currentTarget.dataset.id;
			},
		}
	}
</script>

<style lang="scss">
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 500upx;
		background-color: #11CC99;
	}
	
	.qiun-columns {
		display: flex;
		flex-direction: column !important;
	}
	
	.charts {
		width: 750upx;
		height: 500upx;
		background-color: #11CC99;
	}
	
	.scroll{
		height: 700rpx;
	}
	
	.hm-downloads-card {
	  display: flex;
	  align-items: center;
	  flex-direction: row;
	  justify-content: center;
	  width: 100%;
	  height: 242.21rpx;
	}
	
	.container {
	  box-sizing: border-box;
	  display: flex;
	  align-items: center;
	  flex-direction: row;
	  justify-content: space-between;
	  border-radius: 38.96rpx;
	  padding-right: 56.49rpx;
	  padding-left: 62.34rpx;
	  box-shadow: 0px 10px 30px rgba(209, 213, 223, 0.5);
	  background-color: #00c600;
	  width: 720.78rpx;
	  height: 214.29rpx;
	}
	
	.downloadDetail {
	  display: flex;
	  flex-direction: row;
	}
	
	.downloadDetail-container {
	  display: flex;
	  align-items: flex-start;
	  flex-direction: column;
	  height: 89.61rpx;
	}
	
	.count {
	  opacity: 1;
	  line-height: 46.75rpx;
	  white-space: nowrap;
	  color: #ffffff;
	  font-family: Helvetica;
	  font-size: 31.17rpx;
	  font-weight: normal;
	}
	
	.downloads {
	  opacity: 1;
	  margin-top: 1.95rpx;
	  line-height: 40.91rpx;
	  white-space: nowrap;
	  color: #ffffff;
	  font-family: Helvetica;
	  font-size: 27.27rpx;
	  font-weight: normal;
	}
	
	.downloadImage {
	  width: 272.73rpx;
	  height: 97.4rpx;
	}
	
</style>
